﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using WPFToolBarExample.Helper;
using WPFToolBarExample.Model;
using WPFToolBarExample.View;

namespace WPFToolBarExample.ViewModel
{
  class ApplicationUIViewModel : BaseViewModel
  {
    string _displayText = string.Empty;
    private StudentModel _newStudentModel = null;
    private CourseModel _newCourseModel = null;
    List<BaseViewModel> _listOfObjectsToSave = new List<BaseViewModel>(); 

    public string DisplayText
    {
      get { return _displayText; }
      set
      {
        _displayText = value;
        OnPropertyChanged(() => this.DisplayText);
      }
    }

    public ApplicationUIViewModel()
    {
      this.DisplayText = "Hi Team, " + "\n" + "\nI will be out of office for a week." + "\n" + "\nRegards," + "\n" + "\n Siddharth Mishra";
    }

    #region Commands

    private ICommand _newCourseCommand;

    public ICommand NewCourseCommand
    {
      get
      {
        if (_newCourseCommand == null)
          this._newCourseCommand = new RelayCommand(param => this.NewCourseCommand_Execute(param), param => this.NewCourseCommand_CanExecute(param));
        return _newCourseCommand;
      }

    }

    bool NewCourseCommand_CanExecute(object param)
    {
      return true;
    }

    void NewCourseCommand_Execute(object param)
    {
      var courseView = new NewCourseView();
      _newCourseModel = new CourseModel();
      courseView.DataContext = _newCourseModel;
      _newCourseModel.View = courseView;

      _newCourseModel.ShowDialog();
      _listOfObjectsToSave.Add(_newCourseModel);
    }


    private ICommand _newStudentCommand;

    public ICommand NewStudentCommand
    {
      get
      {
        if (_newStudentCommand == null)
          this._newStudentCommand = new RelayCommand(param => this.NewStudentCommand_Execute(param), param => this.NewStudentCommand_CanExecute(param));
        return _newStudentCommand;
      }

    }

    bool NewStudentCommand_CanExecute(object param)
    {
      return true;
    }

    void NewStudentCommand_Execute(object param)
    {
      var studentView = new NewStudentView();
      _newStudentModel = new StudentModel();
      studentView.DataContext = _newStudentModel;

      _newStudentModel.View = studentView;

      _newStudentModel.ShowDialog();
      _listOfObjectsToSave.Add(_newStudentModel);
    }

    ICommand _newFileCommand;

    public ICommand NewFileCommand
    {
      get
      {
        if (_newFileCommand == null)
          this._newFileCommand = new RelayCommand(param => this.NewFileCommand_Execute(param),
            param => this.NewFileCommand_CanExecute(param));
        return _newFileCommand;
      }
    }

    bool NewFileCommand_CanExecute(object param)
    {
      return true;
    }

    void NewFileCommand_Execute(object param)
    {
      this.DisplayText = string.Empty;
    }

    ICommand _openNewFileCommand;

    public ICommand OpenNewFileCommand
    {
      get
      {
        if (this._openNewFileCommand == null)
          this._openNewFileCommand = new RelayCommand(param => this.OpenNewFileCommand_Execute(param),
            param => this.OpenNewFileCommand_CanExecute(param));
        return _openNewFileCommand;
      }
    }

    bool OpenNewFileCommand_CanExecute(object param)
    {
      return true;
    }

    void OpenNewFileCommand_Execute(object param)
    {
      string initialDir = string.Empty;
      if (null != param)
        initialDir = param.ToString();

      var dialog = new OpenFileDialog();
      dialog.Filter = "TXT files (*.TXT)|*.TXT";
      dialog.FilterIndex = 1;
      dialog.Title = "Please select a text file..";

      dialog.InitialDirectory = string.IsNullOrEmpty(initialDir) ? Environment.GetFolderPath(Environment.SpecialFolder.Desktop) : initialDir;

      DialogResult result = dialog.ShowDialog();
    }

    ICommand _saveCommand;

    public ICommand SaveCommand
    {
      get
      {
        if (null == this._saveCommand)
          //Register command with RelayCommand class.
          this._saveCommand = new RelayCommand(param => this.SaveCommand_Execute(param),
            param => this.SaveCommand_CanExecute(param));
        return _saveCommand;
      }
    }

    bool SaveCommand_CanExecute(object param)
    {
      if (string.IsNullOrEmpty(this.DisplayText))
        return false;

      return true;
    }

    void SaveCommand_Execute(object param)
    {
      //Do some save coding.
      //MessageBox.Show("You have sent '" + param.ToString() + "' as command parameter to save this document.");


      foreach (var baseViewModel in _listOfObjectsToSave)
      {
        baseViewModel.Save();
      }

    }

    #endregion

  }
}
